package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.FileUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.CustomerShareDAO;
import com.feeyin.radar.crm.dao.LinkmanDAO;
import com.feeyin.radar.crm.dao.LinkmanUpdateHistoryDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.dao.SystemFileDAO;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.CustomerShare;
import com.feeyin.radar.crm.model.Linkman;
import com.feeyin.radar.crm.model.QueryCustomer;
import com.feeyin.radar.crm.model.SaleRecord;
import com.feeyin.radar.crm.model.SystemFile;
import com.feeyin.radar.crm.service.SaleRecordService;
import com.feeyin.radar.msg.dao.WarningInfoDAO;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.SaleStageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.SaleStageSet;
import com.feeyin.radar.sys.model.Users;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;

public class SaleRecordServiceImpl
  implements SaleRecordService
{
  private SaleRecordDAO saleRecordDAO;
  private LinkmanDAO linkmanDAO;
  private CustomerDAO customerDAO;
  private SystemFileDAO systemFileDAO;
  private UserDAO userDAO;
  private SaleStageSetDAO saleStageSetDAO;
  private CustomerShareDAO customerShareDAO;
  private WarningInfoDAO warningInfoDAO;
  private DepartmentDAO departmentDAO;
  private LinkmanUpdateHistoryDAO linkmanUpdateHistoryDAO;

  public SaleRecordDAO getSaleRecordDAO()
  {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public LinkmanDAO getLinkmanDAO() {
    return this.linkmanDAO;
  }

  public void setLinkmanDAO(LinkmanDAO linkmanDAO) {
    this.linkmanDAO = linkmanDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public SystemFileDAO getSystemFileDAO() {
    return this.systemFileDAO;
  }

  public void setSystemFileDAO(SystemFileDAO systemFileDAO) {
    this.systemFileDAO = systemFileDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public SaleStageSetDAO getSaleStageSetDAO() {
    return this.saleStageSetDAO;
  }

  public void setSaleStageSetDAO(SaleStageSetDAO saleStageSetDAO) {
    this.saleStageSetDAO = saleStageSetDAO;
  }

  public CustomerShareDAO getCustomerShareDAO() {
    return this.customerShareDAO;
  }

  public void setCustomerShareDAO(CustomerShareDAO customerShareDAO) {
    this.customerShareDAO = customerShareDAO;
  }

  public WarningInfoDAO getWarningInfoDAO() {
    return this.warningInfoDAO;
  }

  public void setWarningInfoDAO(WarningInfoDAO warningInfoDAO) {
    this.warningInfoDAO = warningInfoDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public LinkmanUpdateHistoryDAO getLinkmanUpdateHistoryDAO() {
    return this.linkmanUpdateHistoryDAO;
  }

  public void setLinkmanUpdateHistoryDAO(LinkmanUpdateHistoryDAO linkmanUpdateHistoryDAO)
  {
    this.linkmanUpdateHistoryDAO = linkmanUpdateHistoryDAO;
  }

  public List getPageDeptSaleRecordCustomers(String customerName, Integer salePersonId, Page page, QueryCustomer queryCustomer)
  {
    if (salePersonId == null) return null;
    if (page == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(salePersonId);
    if ((user == null) || (user.getDeptId() == null)) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (StringUtils.isEmpty(deptIds)) return null;

    Customer customer = new Customer();
    customer.setDeptIds(deptIds);
    customer.setCategorys("1,3");
    customer.setIsSaleRecord(Integer.valueOf(1));
    customer.setIsSaleStage("1");
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName2())) {
        customer.setCustomerName(queryCustomer.getCustomerName2());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getCustomerSourceId() != null) && (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(0)) > 0)) {
        if (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(4)) == 0)
          customer.setIsOthersource("1");
        else {
          customer.setCustomerSourceId(queryCustomer.getCustomerSourceId());
        }
      }
      if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }
      if ((queryCustomer.getSaleStageId() != null) && (queryCustomer.getSaleStageId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if ((queryCustomer.getStartPredictCost() != null) && (queryCustomer.getEndPredictCost() != null)) {
        customer.setStartPredictCost(queryCustomer.getStartPredictCost());
        customer.setEndPredictCost(queryCustomer.getEndPredictCost());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartContactDate() != null) && (queryCustomer.getEndContactDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartContactDate());
        customer.setEndLastLinkDate(queryCustomer.getEndContactDate());
      }
    }
    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        user = new Users();
        user = this.userDAO.getUser(cCustomer.getSalePersonId());
        if (user != null) {
          cCustomer.setSalePerson(user.getUserName());
        }

        SaleStageSet saleStage = new SaleStageSet();
        saleStage = this.saleStageSetDAO.getSaleStageByStageCode(cCustomer.getSaleStageId().toString());
        if (saleStage != null) {
          cCustomer.setSaleStage(saleStage.getStageName());
        }

        cCustomer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(cCustomer.getId()));

        CustomerShare customerShare = new CustomerShare();
        customerShare = this.customerShareDAO.getCustomerShareByCustomerIdAndSalePersonId(cCustomer.getId(), salePersonId);
        if ((customerShare != null) && ("1".equals(customerShare.getIsCurShare()))) {
          cCustomer.setIsWrite(customerShare.getIsReadWrite());
        }
      }
    }
    return customers;
  }

  public Integer getPageDeptSaleRecordCustomersCount(String customerName, Integer salePersonId, QueryCustomer queryCustomer)
  {
    if (salePersonId == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(salePersonId);
    if ((user == null) || (user.getDeptId() == null)) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (StringUtils.isEmpty(deptIds)) return null;

    Customer customer = new Customer();
    customer.setDeptIds(deptIds);
    customer.setCategorys("1,3");
    customer.setIsSaleRecord(Integer.valueOf(1));
    customer.setIsSaleStage("1");
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName2())) {
        customer.setCustomerName(queryCustomer.getCustomerName2());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getCustomerSourceId() != null) && (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(0)) > 0)) {
        if (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(4)) == 0)
          customer.setIsOthersource("1");
        else {
          customer.setCustomerSourceId(queryCustomer.getCustomerSourceId());
        }
      }
      if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }
      if ((queryCustomer.getSaleStageId() != null) && (queryCustomer.getSaleStageId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if ((queryCustomer.getStartPredictCost() != null) && (queryCustomer.getEndPredictCost() != null)) {
        customer.setStartPredictCost(queryCustomer.getStartPredictCost());
        customer.setEndPredictCost(queryCustomer.getEndPredictCost());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartContactDate() != null) && (queryCustomer.getEndContactDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartContactDate());
        customer.setEndLastLinkDate(queryCustomer.getEndContactDate());
      }
    }
    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public List getPageMySaleRecordCustomers(String customerName, Integer salePersonId, Page page, QueryCustomer queryCustomer)
  {
    if (salePersonId == null) return null;
    Customer customer = new Customer();

    customer.setCategorys("1,3");
    customer.setPersonId(salePersonId);
    customer.setIsSaleRecord(Integer.valueOf(1));
    customer.setIsSaleStage("1");
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName2())) {
        customer.setCustomerName(queryCustomer.getCustomerName2());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getCustomerSourceId() != null) && (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(0)) > 0)) {
        if (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(4)) == 0)
          customer.setIsOthersource("1");
        else {
          customer.setCustomerSourceId(queryCustomer.getCustomerSourceId());
        }
      }
      if ((queryCustomer.getSaleStageId() != null) && (queryCustomer.getSaleStageId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if ((queryCustomer.getStartPredictCost() != null) && (queryCustomer.getEndPredictCost() != null)) {
        customer.setStartPredictCost(queryCustomer.getStartPredictCost());
        customer.setEndPredictCost(queryCustomer.getEndPredictCost());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartContactDate() != null) && (queryCustomer.getEndContactDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartContactDate());
        customer.setEndLastLinkDate(queryCustomer.getEndContactDate());
      }
    }

    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        Users user = new Users();
        user = this.userDAO.getUser(cCustomer.getSalePersonId());
        if (user != null) {
          cCustomer.setSalePerson(user.getUserName());
        }

        SaleStageSet saleStage = new SaleStageSet();
        saleStage = this.saleStageSetDAO.getSaleStageByStageCode(cCustomer.getSaleStageId().toString());
        if (saleStage != null) {
          cCustomer.setSaleStage(saleStage.getStageName());
        }

        CustomerShare customerShare = new CustomerShare();
        customerShare = this.customerShareDAO.getCustomerShareByCustomerIdAndSalePersonId(cCustomer.getId(), salePersonId);
        if ((customerShare != null) && ("1".equals(customerShare.getIsCurShare()))) {
          cCustomer.setIsWrite(customerShare.getIsReadWrite());
        }
      }
    }
    return customers;
  }

  public Integer getPageMySaleRecordCustomersCount(String customerName, Integer salePersonId, QueryCustomer queryCustomer)
  {
    if (salePersonId == null) return null;
    Customer customer = new Customer();

    customer.setCategorys("1,3");
    customer.setPersonId(salePersonId);
    customer.setIsSaleRecord(Integer.valueOf(1));
    customer.setIsSaleStage("1");
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName2())) {
        customer.setCustomerName(queryCustomer.getCustomerName2());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getCustomerSourceId() != null) && (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(0)) > 0)) {
        if (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(4)) == 0)
          customer.setIsOthersource("1");
        else {
          customer.setCustomerSourceId(queryCustomer.getCustomerSourceId());
        }
      }
      if ((queryCustomer.getSaleStageId() != null) && (queryCustomer.getSaleStageId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if ((queryCustomer.getStartPredictCost() != null) && (queryCustomer.getEndPredictCost() != null)) {
        customer.setStartPredictCost(queryCustomer.getStartPredictCost());
        customer.setEndPredictCost(queryCustomer.getEndPredictCost());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartContactDate() != null) && (queryCustomer.getEndContactDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartContactDate());
        customer.setEndLastLinkDate(queryCustomer.getEndContactDate());
      }
    }

    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public List getSaleRecordsByCustomerId(Integer customerId, int sign, String status) {
    if (customerId == null) return null;
    if (StringUtils.isEmpty(status)) return null;
    Customer customer = new Customer();
    customer = this.customerDAO.getCustomerById(customerId);
    if (customer == null) return null;
    List list = new ArrayList();

    list = this.saleRecordDAO.getSaleRecordsByCustomerIdAndStatus(customerId, status);
    if ((list == null) || (list.size() <= 0)) return null;
    List saleRecords = new ArrayList();
    for (int i = 0; i < list.size(); i++) {
      SaleRecord saleRecord = new SaleRecord();
      saleRecord = (SaleRecord)list.get(i);
      saleRecord.setCustomerId(customer.getId());
      if ((sign == 1) && (saleRecord.getContactDate() != null) && 
        (DateUtils.getHourByDate(saleRecord.getCreateDate()) <= 24L) && (
        (StringUtils.isEmpty(customer.getSaleStatus())) || ("0".equals(customer.getSaleStatus()))))
        saleRecords.add(saleRecord);
      else if ((sign == 0) && (saleRecord.getContactDate() != null)) {
        saleRecords.add(saleRecord);
      }
    }
    return saleRecords;
  }

  public SaleRecord getSaleRecordCustomerInfoByCustomerId(Integer customerId) {
    SaleRecord saleRecord = new SaleRecord();

    if (customerId == null) return saleRecord;
    Customer customer = new Customer();
    customer = this.customerDAO.getCustomerById(customerId);

    if (customer == null) return saleRecord;
    saleRecord.setCustomerId(customer.getId());
    saleRecord.setCustomerName(customer.getCustomerName());
    saleRecord.setOldSaleStageId(customer.getSaleStageId());
    saleRecord.setSaleStageId(customer.getSaleStageId());
    saleRecord.setPredictContract(customer.getPredictContract());
    saleRecord.setPredictCost(customer.getPredictCost());

    saleRecord.setCustomerRequireDesc(customer.getRequireDesc());

    return saleRecord;
  }

  public SaleRecord getSaleRecordByCustomerIdAndSaleRecordId(String realPath, Integer customerId, Integer saleRecordId)
  {
    if (customerId == null) return null;
    if (saleRecordId == null) return null;

    Customer customer = new Customer();
    customer = this.customerDAO.getCustomerById(customerId);

    if (customer == null) return null;
    SaleRecord saleRecord = new SaleRecord();
    saleRecord = this.saleRecordDAO.getSaleRecord(saleRecordId);
    if (saleRecord == null) return null;
    saleRecord.setCreatePerson(this.userDAO.getUserNameById(saleRecord.getCreatePersonId()));
    saleRecord.setSalePerson(this.userDAO.getUserNameById(saleRecord.getSalePersonId()));
    saleRecord.setCustomerId(customer.getId());
    saleRecord.setCustomerName(customer.getCustomerName());
    saleRecord.setOldSaleStageId(customer.getSaleStageId());

    saleRecord.setCustomerRequireDesc(customer.getRequireDesc());

    Linkman linkman = new Linkman();
    linkman = this.linkmanDAO.getLinkman(saleRecord.getLinkmanId());
    if (linkman != null) {
      saleRecord.setLinkman(linkman.getLinkmanName());
      saleRecord.setLinkmanDeptId(linkman.getLinkmanDeptId());
    }

    List systemFiles = new ArrayList();
    systemFiles = this.systemFileDAO.getSystemFilesByRelatedId(saleRecordId);
    if ((systemFiles != null) && (!StringUtils.isEmpty(realPath))) {
      for (int i = 0; i < systemFiles.size(); i++) {
        SystemFile systemFile = new SystemFile();
        systemFile = (SystemFile)systemFiles.get(i);
        systemFile.setPerson(this.userDAO.getUserNameById(systemFile.getPersonId()));
        if (isFileExists(null, realPath + systemFile.getPath(), customerId, saleRecordId, systemFile.getFileName())) {
          saleRecord.getSystemFiles().add(systemFile);
        }
      }
    }

    return saleRecord;
  }

  public List getCloseSaleRcordsByCustomerId(Integer customerId)
  {
    return this.saleRecordDAO.getCloseSaleRcordsByCustomerId(customerId);
  }

  public boolean insertSaleRecord(SaleRecord saleRecord, String realPath, MultipartFile multipartFile) {
    if (saleRecord == null) return false;
    if (saleRecord.getCustomerId() == null) return false;

    boolean result1 = false;
    boolean result2 = false;
    Customer paraCustomer = new Customer();
    paraCustomer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());
    if (paraCustomer == null) return false;

    saleRecord.setSaleRecordCode(saleRecord.getCustomerId() + DateUtils.getDateByFormat("yyyyMMddHHmmss"));

    saleRecord.setCreateDate(new Date());
    saleRecord.setIsRemind("0");
    saleRecord.setCreatePersonId(saleRecord.getCurrentUserId());
    saleRecord.setStatus("0");
    saleRecord.setSalePersonId(paraCustomer.getSalePersonId());
    result1 = this.saleRecordDAO.insertSaleRecord(saleRecord);

    SaleRecord newSaleRecord = new SaleRecord();
    newSaleRecord = this.saleRecordDAO.getSaleRecordBySaleRecordCode(saleRecord.getSaleRecordCode());
    if (newSaleRecord == null) return false;

    Integer saleStageId = saleRecord.getSaleStageId();
    Date lastLinkDate = new Date();
    SaleRecord sr = new SaleRecord();
    sr = this.saleRecordDAO.getSaleRecordLastLinkByCustomerIdStatus(saleRecord.getCustomerId(), "0");
    if (sr != null) {
      if (sr.getContactDate() != null) {
        lastLinkDate = sr.getContactDate();
      }
      if (sr.getSaleStageId() != null) {
        saleStageId = sr.getSaleStageId();
      }
    }

    Customer customer = new Customer();
    customer.setId(saleRecord.getCustomerId());
    customer.setPredictCost(saleRecord.getPredictCost());
    customer.setPredictContract(saleRecord.getPredictContract());

    customer.setSaleStageId(saleStageId);
    customer.setRequireDesc(saleRecord.getCustomerRequireDesc());
    customer.setIsFollow("1");
    customer.setCustomerCategory("1");
    customer.setUpdateSaleRecordDate(lastLinkDate);
    customer.setSaleStatus("0");
    if ((paraCustomer != null) && (paraCustomer.getSaleStageId() != null)) {
      if ((saleRecord.getSaleStageId() != null) && 
        (saleRecord.getSaleStageId().compareTo(paraCustomer.getSaleStageId()) != 0))
        customer.setUpdateSaleStageDate(lastLinkDate);
    }
    else if (saleRecord.getSaleStageId() != null) {
      customer.setUpdateSaleStageDate(lastLinkDate);
    }
    this.customerDAO.updateCustomer(customer);

    this.saleRecordDAO.deleteSaleRecordByCustomerIdAndStatus(saleRecord.getCustomerId(), "3");

    if ((!StringUtils.isEmpty(realPath)) && 
      (multipartFile != null) && (!multipartFile.isEmpty())) {
      String fileName = multipartFile.getOriginalFilename();
      String storedPath = getStoredPath(realPath, newSaleRecord.getCustomerId(), newSaleRecord.getId()) + 
        System.getProperty("file.separator") + fileName;
      result2 = FileUtils.saveFile(storedPath, multipartFile);
      if (result2) {
        SystemFile systemFile = new SystemFile();
        systemFile.setFileName(fileName);
        systemFile.setFileType(multipartFile.getContentType());

        systemFile.setPath(getStoredRelativePath(newSaleRecord.getCustomerId(), newSaleRecord.getId()) + System.getProperty("file.separator"));
        systemFile.setPersonId(saleRecord.getCurrentUserId());
        systemFile.setRelatedId(newSaleRecord.getId());
        systemFile.setRelatedTable("saleRecord");
        systemFile.setSize(multipartFile.getSize()+"");
        systemFile.setUploadDate(new Date());
        this.systemFileDAO.insertSystemFile(systemFile);
      }

    }

    this.warningInfoDAO.insertCurrentScheduleWarningInfo(saleRecord);
    return true;
  }

  public boolean insertSaleRecordHangup(SaleRecord saleRecord, String realPath, MultipartFile multipartFile) throws Exception
  {
    if (saleRecord == null) return false;
    if (saleRecord.getCustomerId() == null) return false;

    boolean result1 = false;
    boolean result2 = false;
    Customer paraCustomer = new Customer();
    paraCustomer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());
    if (paraCustomer == null) return false;

    saleRecord.setSaleRecordCode(saleRecord.getCustomerId() + DateUtils.getDateByFormat("yyyyMMddHHmmss"));
    saleRecord.setLinkmanId(getLinkmanId(saleRecord.getCustomerId(), saleRecord.getLinkmanDeptId(), saleRecord.getLinkman()));
    saleRecord.setCreateDate(new Date());
    saleRecord.setIsRemind("0");
    saleRecord.setCreatePersonId(saleRecord.getCurrentUserId());
    saleRecord.setStatus("3");
    saleRecord.setSalePersonId(paraCustomer.getSalePersonId());
    result1 = this.saleRecordDAO.insertSaleRecord(saleRecord);

    SaleRecord newSaleRecord = new SaleRecord();
    newSaleRecord = this.saleRecordDAO.getSaleRecordBySaleRecordCode(saleRecord.getSaleRecordCode());
    if (newSaleRecord == null) return false;

    Date lastLinkDate = new Date();
    SaleRecord sr = new SaleRecord();
    sr = this.saleRecordDAO.getSaleRecordLastLinkByCustomerIdStatus(saleRecord.getCustomerId(), "0");
    if ((sr != null) && 
      (sr.getContactDate() != null)) {
      lastLinkDate = sr.getContactDate();
    }

    Customer customer = new Customer();
    customer.setId(saleRecord.getCustomerId());

    customer.setUpdateSaleRecordDate(lastLinkDate);
    customer.setSaleStatus("1");
    if (saleRecord.getContactDate() != null)
      customer.setUpdateSaleStageDate(saleRecord.getContactDate());
    else {
      customer.setUpdateSaleStageDate(lastLinkDate);
    }

    this.customerDAO.updateCustomer(customer);

    if ((!StringUtils.isEmpty(realPath)) && 
      (multipartFile != null) && (!multipartFile.isEmpty())) {
      String fileName = multipartFile.getOriginalFilename();
      String storedPath = getStoredPath(realPath, newSaleRecord.getCustomerId(), newSaleRecord.getId()) + 
        System.getProperty("file.separator") + fileName;
      result2 = FileUtils.saveFile(storedPath, multipartFile);
      if (result2) {
        SystemFile systemFile = new SystemFile();
        systemFile.setFileName(fileName);
        systemFile.setFileType(multipartFile.getContentType());

        systemFile.setPath(getStoredRelativePath(newSaleRecord.getCustomerId(), newSaleRecord.getId()) + System.getProperty("file.separator"));
        systemFile.setPersonId(saleRecord.getCurrentUserId());
        systemFile.setRelatedId(newSaleRecord.getId());
        systemFile.setRelatedTable("saleRecord");
        systemFile.setSize(multipartFile.getSize()+"");
        systemFile.setUploadDate(new Date());
        this.systemFileDAO.insertSystemFile(systemFile);
      }

    }

    return true;
  }

  public boolean insertSaleRecordCare(SaleRecord saleRecord, String realPath, MultipartFile multipartFile) throws Exception
  {
    if (saleRecord == null) return false;
    if (saleRecord.getCustomerId() == null) return false;

    boolean result1 = false;
    boolean result2 = false;
    Customer paraCustomer = new Customer();
    paraCustomer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());
    if (paraCustomer == null) return false;

    saleRecord.setSaleRecordCode(saleRecord.getCustomerId() + DateUtils.getDateByFormat("yyyyMMddHHmmss"));
    saleRecord.setLinkmanId(getLinkmanId(saleRecord.getCustomerId(), saleRecord.getLinkmanDeptId(), saleRecord.getLinkman()));
    saleRecord.setCreateDate(new Date());
    saleRecord.setIsRemind("0");
    saleRecord.setCreatePersonId(saleRecord.getCurrentUserId());
    saleRecord.setStatus("2");
    saleRecord.setSalePersonId(paraCustomer.getSalePersonId());
    result1 = this.saleRecordDAO.insertSaleRecord(saleRecord);

    SaleRecord newSaleRecord = new SaleRecord();
    newSaleRecord = this.saleRecordDAO.getSaleRecordBySaleRecordCode(saleRecord.getSaleRecordCode());
    if (newSaleRecord == null) return false;

    if ((!StringUtils.isEmpty(realPath)) && 
      (multipartFile != null) && (!multipartFile.isEmpty())) {
      String fileName = multipartFile.getOriginalFilename();
      String storedPath = getStoredPath(realPath, newSaleRecord.getCustomerId(), newSaleRecord.getId()) + 
        System.getProperty("file.separator") + fileName;
      result2 = FileUtils.saveFile(storedPath, multipartFile);
      if (result2) {
        SystemFile systemFile = new SystemFile();
        systemFile.setFileName(fileName);
        systemFile.setFileType(multipartFile.getContentType());

        systemFile.setPath(getStoredRelativePath(newSaleRecord.getCustomerId(), newSaleRecord.getId()) + System.getProperty("file.separator"));
        systemFile.setPersonId(saleRecord.getCurrentUserId());
        systemFile.setRelatedId(newSaleRecord.getId());
        systemFile.setRelatedTable("saleRecord");
        systemFile.setSize(multipartFile.getSize()+"");
        systemFile.setUploadDate(new Date());
        this.systemFileDAO.insertSystemFile(systemFile);
      }

    }

    this.warningInfoDAO.insertCurrentScheduleWarningInfo(saleRecord);
    return true;
  }

  public boolean insertSaleRecordClose(SaleRecord saleRecord, String realPath, MultipartFile multipartFile) throws Exception
  {
    if (saleRecord == null) return false;
    if (saleRecord.getCustomerId() == null) return false;

    boolean result1 = false;
    boolean result2 = false;
    Customer paraCustomer = new Customer();
    paraCustomer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());
    if (paraCustomer == null) return false;

    saleRecord.setSaleRecordCode(saleRecord.getCustomerId() + DateUtils.getDateByFormat("yyyyMMddHHmmss"));
    saleRecord.setLinkmanId(getLinkmanId(saleRecord.getCustomerId(), saleRecord.getLinkmanDeptId(), saleRecord.getLinkman()));
    saleRecord.setCreateDate(new Date());
    saleRecord.setIsRemind("0");
    saleRecord.setCreatePersonId(saleRecord.getCurrentUserId());
    saleRecord.setStatus("0");
    saleRecord.setSalePersonId(paraCustomer.getSalePersonId());
    result1 = this.saleRecordDAO.insertSaleRecord(saleRecord);

    SaleRecord newSaleRecord = new SaleRecord();
    newSaleRecord = this.saleRecordDAO.getSaleRecordBySaleRecordCode(saleRecord.getSaleRecordCode());
    if (newSaleRecord == null) return false;

    Date lastLinkDate = new Date();
    SaleRecord sr = new SaleRecord();
    sr = this.saleRecordDAO.getSaleRecordLastLinkByCustomerIdStatus(saleRecord.getCustomerId(), "0");
    if ((sr != null) && 
      (sr.getContactDate() != null)) {
      lastLinkDate = sr.getContactDate();
    }

    Customer customer = new Customer();
    customer.setId(saleRecord.getCustomerId());
    customer.setPredictCost(saleRecord.getPredictCost());
    customer.setPredictContract(saleRecord.getPredictContract());

    customer.setSaleStageId(Integer.valueOf(0));
    customer.setRequireDesc(saleRecord.getCustomerRequireDesc());
    customer.setIsFollow("0");

    customer.setUpdateSaleRecordDate(lastLinkDate);
    customer.setSaleStatus("4");
    customer.setUpdateSaleStageDate(lastLinkDate);

    this.customerDAO.updateCustomer(customer);

    this.saleRecordDAO.deleteSaleRecordByCustomerIdAndStatus(saleRecord.getCustomerId(), "3");

    this.saleRecordDAO.updateSaleRecordStatusByCustomerId(saleRecord.getCustomerId(), "0", "1");

    if ((!StringUtils.isEmpty(realPath)) && 
      (multipartFile != null) && (!multipartFile.isEmpty())) {
      String fileName = multipartFile.getOriginalFilename();
      String storedPath = getStoredPath(realPath, newSaleRecord.getCustomerId(), newSaleRecord.getId()) + 
        System.getProperty("file.separator") + fileName;
      result2 = FileUtils.saveFile(storedPath, multipartFile);
      if (result2) {
        SystemFile systemFile = new SystemFile();
        systemFile.setFileName(fileName);
        systemFile.setFileType(multipartFile.getContentType());

        systemFile.setPath(getStoredRelativePath(newSaleRecord.getCustomerId(), newSaleRecord.getId()) + System.getProperty("file.separator"));
        systemFile.setPersonId(saleRecord.getCurrentUserId());
        systemFile.setRelatedId(newSaleRecord.getId());
        systemFile.setRelatedTable("saleRecord");
        systemFile.setSize(multipartFile.getSize()+"");
        systemFile.setUploadDate(new Date());
        this.systemFileDAO.insertSystemFile(systemFile);
      }

    }

    this.warningInfoDAO.insertCurrentScheduleWarningInfo(saleRecord);
    return true;
  }

  public boolean updateSaleRecord(SaleRecord saleRecord, String realPath, MultipartFile multipartFile)
  {
    if (saleRecord == null) return false;
    if (saleRecord.getCustomerId() == null) return false;
    if (saleRecord.getId() == null) return false;

    boolean result1 = false;
    boolean result2 = false;

    saleRecord.setLinkmanId(getLinkmanId(saleRecord.getCustomerId(), saleRecord.getLinkmanDeptId(), saleRecord.getLinkman()));
    saleRecord.setIsRemind("0");
    result1 = this.saleRecordDAO.updateSaleRecord(saleRecord);

    if (result1) {
      Customer paraCustomer = new Customer();
      paraCustomer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());

      Integer saleStageId = saleRecord.getSaleStageId();
      Date lastLinkDate = new Date();
      SaleRecord sr = new SaleRecord();
      sr = this.saleRecordDAO.getSaleRecordLastLinkByCustomerIdStatus(saleRecord.getCustomerId(), "0");
      if (sr != null) {
        if (sr.getContactDate() != null) {
          lastLinkDate = sr.getContactDate();
        }
        if (sr.getSaleStageId() != null) {
          saleStageId = sr.getSaleStageId();
        }
      }

      Customer customer = new Customer();
      customer.setId(saleRecord.getCustomerId());
      customer.setPredictCost(saleRecord.getPredictCost());
      customer.setPredictContract(saleRecord.getPredictContract());

      customer.setSaleStageId(saleStageId);
      customer.setRequireDesc(saleRecord.getCustomerRequireDesc());
      customer.setIsFollow("1");
      customer.setUpdateSaleRecordDate(lastLinkDate);

      if ((paraCustomer != null) && (paraCustomer.getSaleStageId() != null)) {
        if ((saleRecord.getSaleStageId() != null) && 
          (saleRecord.getSaleStageId().compareTo(paraCustomer.getSaleStageId()) != 0))
          customer.setUpdateSaleStageDate(lastLinkDate);
      }
      else if (saleRecord.getSaleStageId() != null) {
        customer.setUpdateSaleStageDate(lastLinkDate);
      }
      this.customerDAO.updateCustomer(customer);

      if ((!StringUtils.isEmpty(realPath)) && 
        (multipartFile != null) && (!multipartFile.isEmpty())) {
        String fileName = multipartFile.getOriginalFilename();
        String storedPath = getStoredPath(realPath, saleRecord.getCustomerId(), saleRecord.getId()) + 
          System.getProperty("file.separator") + fileName;
        result2 = FileUtils.saveFile(storedPath, multipartFile);
        if (result2) {
          SystemFile systemFile = new SystemFile();
          systemFile.setFileName(fileName);
          systemFile.setFileType(multipartFile.getContentType());

          systemFile.setPath(getStoredRelativePath(saleRecord.getCustomerId(), saleRecord.getId()) + System.getProperty("file.separator"));
          systemFile.setPersonId(saleRecord.getCurrentUserId());
          systemFile.setRelatedId(saleRecord.getId());
          systemFile.setRelatedTable("saleRecord");
          systemFile.setSize(multipartFile.getSize()+"");
          systemFile.setUploadDate(new Date());
          this.systemFileDAO.insertSystemFile(systemFile);
        }
      }
    }
    return result1;
  }

  private Integer getLinkmanId(Integer customerId, Integer linkmanDeptId, String linkmanName)
  {
    Integer rLinkmanId = Integer.valueOf(0);
    if (customerId == null) return rLinkmanId;
    if (StringUtils.isEmpty(linkmanName)) return rLinkmanId;

    List list = new ArrayList();
    Linkman linkman = new Linkman();
    linkman.setLinkmanName(linkmanName);
    if (linkmanDeptId != null) {
      linkman.setLinkmanDeptId(linkmanDeptId);
    }
    linkman.setCustomerId(customerId);
    list = this.linkmanDAO.getLinkmansByDynamic(linkman);
    if ((list != null) && (list.size() > 0)) {
      rLinkmanId = ((Linkman)list.get(0)).getId();
    }
    else
    {
      String linkmanCode = customerId + DateUtils.getDateByFormat("yyyyMMddHHmmss");
      linkman = new Linkman();
      linkman.setCustomerId(customerId);
      linkman.setLinkmanCode(linkmanCode);
      linkman.setLinkmanName(linkmanName);
      linkman.setLinkmanDeptId(Integer.valueOf(0));
      linkman.setCreateDate(new Date());
      this.linkmanDAO.insertLinkman(linkman);

      Linkman newLinkman = new Linkman();
      newLinkman = this.linkmanDAO.getLinkmanByLinkmanCode(linkmanCode);
      if (newLinkman != null) {
        rLinkmanId = newLinkman.getId();

        newLinkman.setLinkmanId(newLinkman.getId());
        newLinkman.setUpdatePersonId(linkman.getCurrentUserId());
        newLinkman.setUpdateDate(new Date());
        newLinkman.setIsPrimitive("1");
        this.linkmanUpdateHistoryDAO.insertLinkmanUpdateHistory(newLinkman);
      }
    }
    return rLinkmanId;
  }

  public boolean isFileExists(String basePath, String fullPath, Integer customerId, Integer saleRecordId, String fileName)
  {
    String fullPathName = "";
    if (StringUtils.isEmpty(fullPath))
      fullPathName = basePath + "upload" + System.getProperty("file.separator") + 
        "saleRecord" + System.getProperty("file.separator") + 
        customerId + "_" + saleRecordId + System.getProperty("file.separator") + 
        fileName;
    else {
      fullPathName = fullPath + fileName;
    }

    File file = new File(fullPathName);
    return file.exists();
  }

  public String getStoredPath(String basePath, Integer customerId, Integer saleRecordId)
  {
    String storedPath = basePath + "upload" + System.getProperty("file.separator") + 
      "saleRecord" + System.getProperty("file.separator") + 
      customerId + "_" + saleRecordId;
    File file = new File(storedPath);
    if ((file.exists()) && (file.isDirectory()))
    {
      return storedPath;
    }
    file.mkdirs();
    return storedPath;
  }

  public String getStoredRelativePath(Integer customerId, Integer saleRecordId)
  {
    String storedPath = "upload" + System.getProperty("file.separator") + 
      "saleRecord" + System.getProperty("file.separator") + 
      customerId + "_" + saleRecordId;
    File file = new File(storedPath);
    if ((file.exists()) && (file.isDirectory()))
    {
      return storedPath;
    }
    file.mkdirs();
    return storedPath;
  }

  public boolean deleteAdded(String realPath, Integer addedId)
  {
    if (addedId == null) return false;
    if (StringUtils.isEmpty(realPath)) return false;
    boolean result = false;
    SystemFile systemFile = new SystemFile();
    systemFile = this.systemFileDAO.getSystemFile(addedId);
    if (systemFile == null) return false;
    File file = new File(realPath + systemFile.getPath() + systemFile.getFileName());
    if (file.exists()) {
      result = file.delete();
    }
    this.systemFileDAO.deleteSystemFile(addedId);
    return result;
  }

  public Integer getCurrentDateRecordCount(Integer userId) {
    SaleRecord sr = new SaleRecord();
    sr.setSalePersonId(userId);
    Date now = new Date();
    Date currentDate = null;
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      currentDate = sdf.parse(sdf.format(now));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    sr.setCurrentDate(currentDate);
    return this.saleRecordDAO.getCurrentDateRecordCount(sr);
  }
}